@include('admin.head')
<div class="container">
	<div class="row">
		@include('admin.side')
		<div class="col-md-10" id="content-wrapper">
			<ul class="nav nav-tabs">
			  <li class="active"><a href="#home" data-toggle="tab" aria-expanded="false">设备基础信息</a></li>
			  <li class=""><a href="#case" data-toggle="tab" id="showCase" aria-expanded="true">设备案列</a></li>
			  <li class=""><a href="#project" data-toggle="tab" id="showApply" aria-expanded="true">应用项目</a></li>
			</ul>
			<div id="myTabContent" class="tab-content">
			 	<div class="tab-pane fade active in" id="home">
					<div class="row">
						<div class="col-xs-12">
							<div class=" clearfix">
								<div class="clearfix">
									<h2 class="pull-left">设备编辑</h2>
								</div>
								<div class="panel panel-default">
									<div class="panel-body">
										<div class="form-horizontal">
										  <fieldset>
										    <div class="form-group">
										      <label class="col-xs-4  control-label no_padding_right">设备名称:</label>
										      <div class="col-xs-3">
										        <input class="form-control input_size" name="name"  placeholder="" type="text">
										      </div>
										    </div>

										    <div class="form-group">
										      <label class="col-xs-4  control-label no_padding_right">设备型号:</label>
										      <div class="col-xs-3">
										        <input class="form-control input_size" name="model"  placeholder="" type="text">
										      </div>
										    </div>

										    <div class="form-group">
										      <label class="col-xs-4  control-label no_padding_right">制造商:</label>
										      <div class="col-xs-3">
										        <input class="form-control input_size" name="creator"  placeholder="" type="text">
										      </div>
										    </div>

										    <div class="form-group">
										      <label class="col-xs-4  control-label no_padding_right">认证信息:</label>
										      <div class="col-xs-3">
										        <input class="form-control input_size" name="auth_info"  placeholder="" type="text">
										      </div>
										    </div>


										    <div class="form-group">
										      <label class="col-xs-4  control-label no_padding_right">产品标准:</label>
										      <div class="col-xs-3">
										        <input class="form-control input_size" name="standard"  placeholder="" type="text">
										      </div>
										    </div>

										    <div class="form-group">
										      <label class="col-xs-4  control-label no_padding_right">设备最低价格:</label>
										      <div class="col-xs-3">
										        <input class="form-control input_size" name="minimize_price"  placeholder="" type="text">
										      </div>
										    </div>

										    <div class="form-group">
										      <label class="col-xs-4  control-label no_padding_right">设备最高价格:</label>
										      <div class="col-xs-3">
										        <input class="form-control input_size" name="maximize_price"  placeholder="" type="text">
										      </div>
										    </div>

								    	    <div class="form-group">
								          		<label for="select" class="col-xs-4  control-label no_padding_right">应用技术:</label>
								    	      	<div class="col-xs-3">
								    	        <select class="form-control input_size" name="tech_id">
								    	          <option value="">请选择</option>
								    	        </select>
								    	      </div>
								        	</div>

								        	<div class="form-group" >
								          		<label for="select" class="col-xs-4  control-label no_padding_right">应用领域:</label>
								    	      	<div class="col-xs-3">
								    	        <select class="form-control input_size" name="domain_id" id="domain" style="margin-bottom:10px">
								    	          <option value="">请选择</option>
								    	        </select>
								    	        <select class="form-control input_size" name="domain_ids" id="">
								    	          <option value="">请选择</option>
								    	        </select>
								    	     	</div>
								        	</div>

						        		    <div class="form-group mainBody">
						        		      <label class="col-xs-4  control-label no_padding_right">应用主体:</label>
						        		      <div class="col-xs-3">
						        		        <select class="form-control input_size main_body" name="mainbody_id">
								    	          	<option value="">请选择</option>
						        		        </select>
						        		      </div>
						        		      <div class="col-xs-4  no_padding_both">
						        		      	<button class="btn btn-primary" id="addMain">添加</button>
						        		      </div>
						        		    </div>

										    <div class="form-group">
										      <label class="col-xs-4  control-label no_padding_right">设备图片:</label>
										      <div class="col-xs-5">
										        <a href="javascript:;">
										            <input type="file" name="imgFile" id="pic">
										            <input type="hidden" class="hiddenName" name="image">
										        </a><span class="fileName"></span>
										      </div>
										    </div>

										    <div class="form-group">
										      <label for="inputEmail" class="col-xs-4  control-label no_padding_right">节能效率:</label>
										      <div class="col-xs-3">
										        <input class="form-control input_size" name="efficiency" autocomplete="of" id="inputEmail" placeholder="" type="text">
										      </div>
										    </div>

										    <div class="form-group">
										      <label for="textArea" class="col-xs-4  control-label no_padding_right">设备描述:</label>
										      <div class="col-xs-3">
										        <textarea class="form-control text_size" rows="3" name="description" id="textArea"></textarea>
										        <span class="help-block"></span>
										      </div>
										    </div>

									    	<div class="form-group">
									    		<label for="textArea" class="col-xs-4  control-label no_padding_right">设备原理:</label>
									          	<div class="col-xs-7">
									            	<div id="container" style="height:500px;"></div>
									          	</div>
									        </div>

										    <div class="form-group">
										      <div class="col-xs-12 text-center">
										      	<input type="hidden" name="did">
										        <button type="button" id="sub" class="btn btn-primary ">提交</button>
										      </div>
										    </div>
										  </fieldset>
										</div>
									</div>
								</div>
							</div>
						</div>
					</div>
			  	</div>
			  	<div class="tab-pane fade" id="case">
			    	<div class="panel panel-default">
							<div class="panel-body">
								<div class="form-horizontal">
								  <fieldset>
								    <div class="form-group">
								      <label class="col-xs-3 control-label no_padding_right">案例名称:</label>
								      <div class="col-xs-6">
								        <input class="form-control input_size" name="name"  placeholder="" type="text">
								      </div>
								    </div>

								    <div class="form-group">
								      <label for="textArea" class="col-xs-3 control-label no_padding_right">案例描述:</label>
								      <div class="col-xs-6">
								        <textarea class="form-control text_size" rows="3" name="content" id="textArea"></textarea>
								        <span class="help-block"></span>
								      </div>
								    </div>

								    <div class="form-group">
								      <label class="col-xs-3 control-label no_padding_right">开始时间:</label>
								      <div class="col-xs-6">
								        <input class="form-control input_size date-time-picker" readonly name="start_time"  placeholder="" type="text">
								      </div>
								    </div>

								    <div class="form-group">
								      <label class="col-xs-3 control-label no_padding_right">结束时间:</label>
								      <div class="col-xs-6">
								        <input class="form-control input_size date-time-picker" readonly name="end_time"  placeholder="" type="text">
								      </div>
								    </div>

								    <div class="form-group">
								      <div class="col-xs-12 text-center">
								      	<input type="hidden" name="cid">
								        <button type="button" id="addCase" class="btn btn-primary ">添加</button>
								      </div>
								    </div>

								      	<table class="table table-striped table-hover text-center table_marginTop">
								    	  <thead>
								    	      <td><b>案例名称</b></td>
								    	      <td><b>案例内容</b></td>
								    	      <td><b>开始时间</b></td>
								    	      <td><b>结束时间</b></td>
								          	  <td><b>操作</b></td>
								    	  </thead>
								    	  <tbody id="casesLists">
								    	  </tbody>
								    	</table>
								  </fieldset>
								</div>
							</div>
					</div>
			  	</div>
			  	<div class="tab-pane fade" id="project">
		    		<div class="panel panel-default">
			    		<div class="panel-body">
			    			<div class="form-horizontal">
			    			  	<fieldset>
			            		    <div class="form-group applyProject">
			            		      <label class="col-xs-3 control-label no_padding_right">应用案例:</label>
			            		      <div class="col-xs-6">
			            		        <select class="form-control input_size" id="apply_project">
			    		    	          	<option value="">请选择</option>
			            		        </select>
			            		      </div>
			            		      <div class="col-xs-3 no_padding_both">
			            		      	<button class="btn btn-primary" id="addApply">添加</button>
			            		      </div>
			            		    </div>
		            		      	<table class="table table-striped table-hover text-center table_marginTop">
		            		     	<thead>
		            		    	    <td><b>案例项目名称</b></td>
		            		    	    <td><b>开始时间</b></td>
		            		    	    <td><b>结束时间</b></td>
		            		          	<td><b>操作</b></td>
		            		    	</thead>
		            		    	<tbody id="applyLists">
		            		    	</tbody>
		            		    	</table>
			    			  	</fieldset>
			    			</div>
			    		</div>
			    	</div>
			  	</div>
			</div>
		</div>
	</div>
</div>
@include("admin.footer")
@include("admin.common")
</body>

<script type="text/html" id="applyList">
{%each list as value index}
<tr>
  <td>{%value.project.name}</td>
  <td>{%value.project.start_time}</td>
  <td>{%value.project.end_time}</td>
  <td>
  	<button class="btn btn-primary deleteApply" aid="{%value.id}" title="删除">
  		<i class="fa fa-trash-o"></i>
  	</button>
  </td>
</tr>
{%/each}
</script>

<script type="text/html" id="applyTemp">
{%each list as value}
	<option value="{%value.id}">{%value.name}</option>
{%/each}
</script>


<script type="text/html" id="mainbodyTemp">
<option value="">请选择</option>
{%each list as value}
<option value="{%value.id}">{%value.name}</option>
{%/each}
</script>

<script type="text/html" id="domainTemp">
<option value="">请选择</option>
{%each list as value}
<option value="{%value.id}">{%value.cate_name}</option>
{%/each}
</script>

<script type="text/html" id="techTemp">
<option value="">请选择</option>
{%each list as value}
<option value="{%value.id}">{%value.cate_name}</option>
{%/each}
</script>

<script type="text/html" id="mainSelectTemp">
<div class="form-group mainBody">
  <label class="col-xs-4 control-label no_padding_right"></label>
  <div class="col-xs-3">
    <select class="form-control input_size main_body">
    </select>
  </div>
  <div class="col-xs-4 no_padding_both">
  	<button class="btn btn-warning deleteMain">删除</button>
  </div>
</div>
</script>

<script type="text/html" id="domain_type">
	<select class="form-control input_size" name="domain_ids">
		{%each list as value index}
		<option value="{%value.id}">{%value.device_domain.domain.cate_name}</option>
		{%/each}
	</select>
</script>

<script type="text/html" id="casesList">
{%each list as value index}
<tr>
  <td>{%value.name}</td>
  <td>{%truncateText(value.content, 20) }</td>
  <td>{%value.start_time}</td>
  <td>{%value.end_time}</td>
  <td>
  	<button class="btn btn-primary editCase" cid="{%value.id}" title="编辑">
  		<i class="fa fa-pencil-square-o"></i>
  	</button>
  	<button class="btn btn-primary deleteCase" cid="{%value.id}" title="删除">
  		<i class="fa fa-trash-o"></i>
  	</button>
  </td>
</tr>
{%/each}
</script>

<script type="text/html" id="projectList">
{%each list as value index}
<tr>
  <td>{%value.name}</td>
  <td>
  	<button class="btn btn-primary deleteCase" cid="{%value.id}" title="删除">
  		<i class="fa fa-trash-o"></i>
  	</button>
  </td>
</tr>
{%/each}
</script>

<script type="text/html" id="applyList">
{%each list as value index}
<tr>
  <td>{%value.project.name}</td>
  <td>{%value.project.start_time}</td>
  <td>{%value.project.end_time}</td>
  <td>
  	<button class="btn btn-primary deleteApply" aid="{%value.id}" title="删除">
  		<i class="fa fa-pencil-square-o"></i>
  	</button>
  </td>
</tr>
{%/each}
</script>
<script type="text/javascript">

waitingDialog.show('正在加载中...', {dialogSize: 'sm', progressType: 'primary'});

var url = "<%url('admin')%>/";
var ue = UE.getEditor('container');
var q = "MyQueue";
var mainBodys;

$.getq(q,url+"get_mainbody", function(data){
	if(data.success){
		mainBodys = data.data;
	}
})

$.get(url+"get_all_project",function (data){
	if(data.success){
		var str = template("applyTemp",{list : data.data});
		$("#apply_project").html(str);
	}
})



// 获取应用技术
$.getq(q,url+"get_techs",function(data){
	if(data.success){
		var html = template("techTemp", {list : data.data});
		$("select[name='tech_id']").html(html);
	}
})

$.getq(q,url+"get_domains",function(data){
	if(data.success){
		var html = template("domainTemp",{list : data.data});
		$("select[name='domain_id']").html(html);
	}
})

$.getq(q,url+"get_mainbody",function(data){
	if (data.success){
		var html = template("mainbodyTemp",{list : data.data});
		$("select[name='mainbody_id']").html(html);
	};
})

$("#addMain").bind("click", function(){
	var html = template("mainSelectTemp", {});
	$(html).insertAfter($(".mainBody").last());
	var str = template("mainbodyTemp", {list : mainBodys});
	$(".main_body").last().html(str);
	$(".deleteMain").unbind("click");
	$(".deleteMain").bind("click", function(){
		$(this).parents(".mainBody").remove();
	})
})



// 图片上传
$('#pic').AjaxFileUpload({
	path : url+"upload",
	onComplete : function(filename){
			$("#home input[name='image']").val(filename.filename);
			$("#home span[class='fileName']").html(filename.filename);
		}
	});

// 应用领域
var childDomainId;
var appDomainSelect;
$("#domain").bind("change", function(){
	appDomainSelect = $(this);
	var parent_id = appDomainSelect.val();
	$.getq(q,url+"get_domains", {parent_id : parent_id}, function (data){
		if(data.success){
				var html = template("domainTemp", {list: data.data});
				appDomainSelect.next().removeClass("hide");
				appDomainSelect.next().html(html);
				$("#home select[name='domain_ids']").val(childDomainId);
			}
	})
});




if("<%Input::has('did')%>"){
	var did = "<%Input::get('did')%>";
	$.getq(q,url+"device/get_detail", {did :did},function(data){
		waitingDialog.hide();
		if(data.success){
			console.log(data)
			$("#home input[name='name']").val(data.name);
			$("#home input[name='model']").val(data.model);
			$("#home input[name='creator']").val(data.creator);
			$("#home input[name='auth_info']").val(data.auth_info);
			$("#home input[name='standard']").val(data.standard);
			$("#home input[name='minimize_price']").val(data.minimize_price);
			$("#home input[name='maximize_price']").val(data.maximize_price);
			$("#home input[name='efficiency']").val(data.efficiency);
			$("#home input[name='image']").val(data.image);
			$("#home select[name='tech_id']").val(data.device_tech.cate_id);
			$("#home select[name='domain_id']").val(data.device_domain.domain.parent_id);
			childDomainId = data.device_domain.domain.id;
			$("#home select[name='domain_id']").change();
			$("#home input[name='did']").val(data.id);
			$("#home textarea[name='description']").val(data.description);
			$.each(data.mainbody, function(k, v){
				$("#home .main_body").eq(k).val(v.mb_id);
				$("#home #addMain").click();
			})
			$("#home .deleteMain").last().click();


			ue.ready(function(){
			    ue.setContent(decodeURIComponent(data.theory));
			});
		}
	})
}

$("#addApply").click(function(){
	var pid = $("#apply_project").val();
	if(pid < 1){
		warning("", "请选择应用项目");
		return;
	}
	$.post(url+"device/edit_device/save_apply", {pid :pid, did : did}, function (data){
		var html = template("applyList", {list : data.data});
		$("#applyLists").html(html);
		$(".deleteApply").unbind("click");
		$(".deleteApply").bind("click", function(){
			deleteApply($(this));
		})
	})
})

$("#showApply").click(function(){
	$.get(url+"device/edit_device/get_device_apply", {did : did}, function (data){
		if(data.success && data.data.length){
			var html = template("applyList", {list : data.data});
			$("#applyLists").html(html);
			$(".deleteApply").unbind("click");
			$(".deleteApply").bind("click", function(){
				deleteApply($(this));
			})
		}
	})
})

var curl = url+"device/edit_device/add_case";
$("#showCase").click(function(){
	$.get(url+"device/edit_device/get_device_cases", {did : did}, function (data){
		if(data.success && data.data.length){
			var html = template("casesList", {list : data.data});
			$("#casesLists").html(html);
			$(".editCase").bind("click", function(){
				editCase($(this));
			})
			$(".deleteCase").bind("click", function(){
				deleteCase($(this));
			})


		}
	})
})
$("#showCase").click(function(){
	$.get(url+"device/edit_device/get_device_project", {did : did}, function (data){
		if(data.success && data.data.length){
			var html = template("casesList", {list : data.data});
			$("#casesLists").html(html);
			$(".editCase").bind("click", function(){
				editCase($(this));
			})
			$(".deleteCase").bind("click", function(){
				deleteCase($(this));
			})


		}
	})
})


$("#addCase").click(function(){
	var caseName = $("#case input[name='name']").val().trim();
	var caseContent = $("#case textarea[name='content']").val().trim();
	var caseStart = $("#case input[name='start_time']").val().trim();
	var caseEnd = $("#case input[name='end_time']").val().trim();
	var cid = $("#case input[name='cid']").val().trim();
	if( caseName == '' || caseContent == '' || caseStart == '' || caseEnd == '' )
	{
		warning('','信息所填不完整');
		return;
	}
	$.post(curl, {
		name : caseName,
		content : caseContent,
		start_time : caseStart,
		end_time : caseEnd,
		cid : cid,
		device_id : did
	}, function (data){
		if(data.success){
			$("#case input[name='name']").val('');
			$("#case textarea[name='content']").val('');
			$("#case input[name='start_time']").val('');
			$("#case input[name='end_time']").val('');
			$("#case input[name='cid']").val('');
			curl = url+"device/edit_device/add_case";
			var html = template("casesList", {list : data.data});
			$("#casesLists").html(html);
			$(".editCase").unbind("click");
			$(".editCase").bind("click", function(){
				editCase($(this));
			})
			$(".deleteCase").unbind("click");
			$(".deleteCase").bind("click", function(){
				deleteCase($(this));
			})
		}
	})
})

function editCase(_this){
	var cid = _this.attr("cid");
	$.get(url+"device/edit_device/get_case_detail", {cid : cid}, function (data){
		$("#case input[name='name']").val(data.data.name);
		$("#case textarea[name='content']").val(data.data.content);
		$("#case input[name='start_time']").val(data.data.start_time);
		$("#case input[name='end_time']").val(data.data.end_time);
		$("#case input[name='cid']").val(cid);
		curl = url+"device/edit_device/update_case";
	})
}

function deleteCase(_this){
	var cid = _this.attr("cid");
	$.post(url+"device/edit_device/delete_cases", {cid : cid, did : did}, function (data){
		if(data.success){
			var html = template("casesList", {list : data.data});
			$("#casesLists").html(html);
			$(".deleteCase").unbind("click");
			$(".deleteCase").bind("click", function(){
				deleteCase($(this));
			})
			$(".editCase").unbind("click");
			$(".editCase").bind("click", function(){
				editCase($(this));
			})
		}
	})
}

function deleteApply(_this){
	var aid = _this.attr("aid");
	$.post(url+"device/edit_device/delete_apply", {aid : aid, did : did}, function (data){
		if(data.success){
			var html = template("applyList", {list : data.data});
			$("#applyLists").html(html);
			$(".deleteApply").unbind("click");
			$(".deleteApply").bind("click", function(){
				deleteApply($(this));
			});
		}
	})
}



$("#sub").click(function(){
	var domain = $("select[name='domain_id']").val();
	var domains = $("select[name='domain_ids']").val();
	if(domains)
	{
		var domain_id = domains;
	}
	else
	{
		var domain_id = domain;
	}
	var tech_id = $("select[name='tech_id']").val();
	var name = $("input[name='name']").val().trim();
	var model = $("input[name='model']").val().trim();
	var creator = $("input[name='creator']").val().trim();
	var auth_info = $("input[name='auth_info']").val().trim();
	var standard = $("input[name='standard']").val().trim();
	var minimize_price = $("input[name='minimize_price']").val().trim();
	var maximize_price = $("input[name='maximize_price']").val().trim();
	var efficiency = $("input[name='efficiency']").val().trim();
	var image = $("input[name='image']").val().trim();
	var did = $("input[name='did']").val().trim();
	var description = $("textarea[name='description']").val().trim();
	var theory = encodeURIComponent(ue.getContent());

	var main_body = [];
	$(".main_body").each(function(k, v){
		if($(v).val() > 0){
			main_body.push($(v).val());
		}
	})


	if( tech_id == '' || name == '' || model == '' || creator == '' || auth_info == '' || standard == '' || minimize_price == '' || maximize_price =='' || efficiency == '' || image == '' || did == '' || description == '' || theory == '')
	{
		warning('','必填信息不能为空');
		return false;
	}
data = {
		name : name,
		model : model,
		creator : creator,
		auth_info : auth_info,
		standard : standard,
		minimize_price : minimize_price,
		maximize_price : maximize_price,
		efficiency : efficiency,
		image : image,
		description : description,
		theory : theory,
		domain_id : domain_id,
		tech_id : tech_id,
		did : did,
		main_body : main_body
	};
	$.postq(q,url+"device/update_device" ,data, function (data){
		if(data.success){
			success('', '提交成功');
			setTimeout(function(){
				window.location.href=" "+url+" device";
			},2000)
		}
	})
})
</script>

</html>